package cn.edu.nju.askinginhand.activity;

import android.app.Activity;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;
import cn.edu.nju.askinginhand.util.LocationGetter;

/**
 * 
 * @author ZhangZu
 * 
 */
public class PictureQuesActivity extends Activity {
	// takepicture panel widgets
	private TextView captureText = null;
	private Button startCaptureBtn = null;
	private Button previewBtn = null;
	// problem panel widgets
	private View problemPanel;
	private EditText titleEdit = null;
	private Spinner firstSpinner = null;
	private Spinner secondSpinner = null;
	private Button publishBtn = null;

	private int status = 0;// 0:ready 1:take over
	private String[] statusTexts = { "准备就绪", "拍摄完成" };
	private String[] buttonTexts = { "拍摄", "重新拍摄" };
	private int[] visibles = { View.INVISIBLE, View.VISIBLE };
	
	private final static String[] class1 = { "不分类", "山东", "江苏" };
	private final static String[][] class2 = { 
			{}, 
			{ "不分类","青岛", "济南" },
			{ "不分类","南京", "无锡" } 
	};
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.picture_ques);
		captureText = (TextView) findViewById(R.id.captureStatusText);
		startCaptureBtn = (Button) findViewById(R.id.startCaptureBtn);
		previewBtn = (Button) findViewById(R.id.previewBtn);
		problemPanel = (View) findViewById(R.id.problemPanel);
		titleEdit = (EditText) findViewById(R.id.titleEdit);
		firstSpinner = (Spinner) findViewById(R.id.firstSpinner);
		secondSpinner = (Spinner) findViewById(R.id.secondSpinner);
		publishBtn = (Button) findViewById(R.id.publishBtn);

		initStartCaptureBtn();
		initPreviewBtn();
		initPublishBtn();
		initFirstSpinner();
		updateAllWidgets();
	}

	public void initStartCaptureBtn() {
		startCaptureBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				//TODO: enter capture UI

				status = 1;
				updateAllWidgets();
			}
		});
	}

	public void initPreviewBtn() {
		previewBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				/**
				 * preview
				 */
			}
		});
	}

	public void initFirstSpinner() {
		ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, R.layout.questype_spinner_item,
				R.id.spinnerText, class1);

		firstSpinner.setAdapter(adapter1);
		firstSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long id) {
				ArrayAdapter<String> adapter2 = null;
				if (position == 0) {
					secondSpinner.setVisibility(View.INVISIBLE);
				} else {
					secondSpinner.setVisibility(View.VISIBLE);
					adapter2 = new ArrayAdapter<String>(PictureQuesActivity.this,
							R.layout.questype_spinner_item, R.id.spinnerText,
							class2[position]);
					secondSpinner.setAdapter(adapter2);
				}
			}

			public void onNothingSelected(AdapterView<?> arg0) {
				secondSpinner.setVisibility(View.INVISIBLE);
			}
		});
	}
	
	public void initPublishBtn() {
		publishBtn.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {

				/**
				 * TODO: publish the problem.
				 * With the location information
				 */
				//Location location = LocationGetter.getInstance().getLocation();
			}
		});
	}

	// called when after capture
	public void updateAllWidgets() {
		captureText.setText(statusTexts[status]);
		startCaptureBtn.setText(buttonTexts[status]);
		previewBtn.setVisibility(visibles[status]);
		//problemPanel.setVisibility(visibles[status]);
	}
}
